
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>第一节 张量、计算图、会话 · Tensorflow学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="scottdu">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="section1.2.html" />
    
    
    <link rel="prev" href="./" />
    

    
    <link rel="stylesheet" href="../gitbook/gitbook-plugin-chart/c3/c3.min.css">
    <script src="../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
    

    <script src="../gitbook/gitbook-plugin-graph/d3.min.js"></script>
    <script src="../gitbook/gitbook-plugin-graph/function-plot.js"></script>    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="./">
            
                <a href="./">
            
                    
                    第一章 Tensorflow框架
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.2.1" data-path="section1.1.html">
            
                <a href="section1.1.html">
            
                    
                    第一节 张量、计算图、会话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="section1.2.html">
            
                <a href="section1.2.html">
            
                    
                    第二节 前向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="section1.3.html">
            
                <a href="section1.3.html">
            
                    
                    第三节 反向传播
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="section1.4.html">
            
                <a href="section1.4.html">
            
                    
                    第四节 搭建神经网络的步骤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter2/">
            
                <a href="../chapter2/">
            
                    
                    第二章 神经网络优化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter2/section2.1.html">
            
                <a href="../chapter2/section2.1.html">
            
                    
                    第一节 损失函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter2/section2.2.html">
            
                <a href="../chapter2/section2.2.html">
            
                    
                    第二节 学习率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter2/section2.3.html">
            
                <a href="../chapter2/section2.3.html">
            
                    
                    第三节 滑动平均
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter2/section2.4.html">
            
                <a href="../chapter2/section2.4.html">
            
                    
                    第四节 正则化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter2/section2.5.html">
            
                <a href="../chapter2/section2.5.html">
            
                    
                    第五节 神经网络的搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter3/">
            
                <a href="../chapter3/">
            
                    
                    第三章 全连接网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter3/section3.1.html">
            
                <a href="../chapter3/section3.1.html">
            
                    
                    第一节 MINIST数据
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter3/section3.2.html">
            
                <a href="../chapter3/section3.2.html">
            
                    
                    第二节 模块化搭建神经网络方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter3/section3.3.html">
            
                <a href="../chapter3/section3.3.html">
            
                    
                    第三节 手写数字识别准确率输出
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter4/">
            
                <a href="../chapter4/">
            
                    
                    第四章 全连接网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter4/section4.1.html">
            
                <a href="../chapter4/section4.1.html">
            
                    
                    第一节 输入手写数字图片输出识别结果
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter4/section4.2.html">
            
                <a href="../chapter4/section4.2.html">
            
                    
                    第二节 制作数据集
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    第五章 卷积网络基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter5/section5.1.html">
            
                <a href="../chapter5/section5.1.html">
            
                    
                    第一节 卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter5/section5.2.html">
            
                <a href="../chapter5/section5.2.html">
            
                    
                    第二节 lenel5代码讲解
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    第六章 卷积网络实践
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/section6.1.html">
            
                <a href="../chapter5/section6.1.html">
            
                    
                    第一节 复现已有的卷积神经网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter6/section6.2.html">
            
                <a href="../chapter6/section6.2.html">
            
                    
                    第二节 用vgg16实现图片识别
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    第七章 Tensorflow应用
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >第一节 张量、计算图、会话</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x5F20;&#x91CF;&#x3001;&#x8BA1;&#x7B97;&#x3001;&#x4F1A;&#x8BDD;">&#x5F20;&#x91CF;&#x3001;&#x8BA1;&#x7B97;&#x3001;&#x4F1A;&#x8BDD;</h1>
<h2 id="&#x57FA;&#x672C;&#x6982;&#x5FF5;">&#x57FA;&#x672C;&#x6982;&#x5FF5;</h2>
<ul>
<li>&#x57FA;&#x4E8E;<code>Tensorflow</code>&#x7684;<strong>&#x795E;&#x7ECF;&#x7F51;&#x7EDC;</strong>&#xFF1A;&#x7528;&#x5F20;&#x91CF;&#x8868;&#x793A;&#x6570;&#x636E;&#xFF0C;&#x7528;&#x8BA1;&#x7B97;&#x56FE;&#x642D;&#x5EFA;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#xFF0C;&#x7528;&#x4F1A;&#x8BDD;&#x6267;&#x884C;&#x8BA1;&#x7B97;&#x56FE;&#xFF0C;&#x4F18;&#x5316;&#x7EBF;&#x4E0A;&#x7684;&#x6743;&#x91CD;(&#x53C2;&#x6570;)&#xFF0C;&#x5F97;&#x5230;&#x6A21;&#x578B;&#x3002;</li>
</ul>
<ul>
<li><strong>&#x5F20;&#x91CF;</strong>&#xFF1A;&#x5F20;&#x91CF;&#x5C31;&#x662F;&#x591A;&#x7EF4;&#x6570;&#x7EC4;(&#x5217;&#x8868;)&#xFF0C;&#x7528;<strong>&#x201C;&#x9636;&#x201D;</strong>&#x8868;&#x793A;&#x5F20;&#x91CF;&#x7684;&#x7EF4;&#x5EA6;&#x3002;</li>
</ul>
<table>
<thead>
<tr>
<th>&#x7EF4;&#x6570;</th>
<th>&#x9636;</th>
<th>&#x540D;&#x5B57;</th>
<th>&#x4F8B;&#x5B50;</th>
</tr>
</thead>
<tbody>
<tr>
<td>0-D</td>
<td>0</td>
<td>&#x6807;&#x91CF; scalar</td>
<td>s=123</td>
</tr>
<tr>
<td>1-D</td>
<td>1</td>
<td>&#x5411;&#x91CF; vector</td>
<td>v=[1,2,3]</td>
</tr>
<tr>
<td>2-D</td>
<td>2</td>
<td>&#x77E9;&#x9635; matrix</td>
<td>m=[[1,2,3],[4,5,6],[7,8,9]]</td>
</tr>
<tr>
<td>n-D</td>
<td>n</td>
<td>&#x5F20;&#x91CF; tensor</td>
<td>t=[[[&#x2026; &#xFF08;n&#x4E2A;&#x4E2D;&#x62EC;&#x53F7;&#xFF09;</td>
</tr>
</tbody>
</table>
<p>&#x5F20;&#x91CF;&#x53EF;&#x4EE5;&#x8868;&#x793A;0&#x9636;&#x5230;n&#x9636;&#x6570;&#x7EC4;(&#x5217;&#x8868;)</p>
<ul>
<li><strong>&#x6570;&#x636E;&#x7C7B;&#x578B;</strong>&#xFF1A;<code>tf.float32</code> <code>tf.int32</code> ...</li>
</ul>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
a = tf.constant([<span class="hljs-number">1.0</span>,<span class="hljs-number">2.0</span>])
b = tf.constant([<span class="hljs-number">3.0</span>,<span class="hljs-number">4.0</span>])
result = a + b
print(result)
</code></pre>
<p>&#x663E;&#x793A;</p>
<pre><code>Tensor(&quot;add:0&quot;, shape=(2,), dtype=float.32)
</code></pre><p>&#x5176;&#x4E2D;<code>add:</code>&#x8868;&#x793A;<code>&#x8282;&#x70B9;&#x540D;</code>&#xFF0C;<code>0</code>&#x8868;&#x793A;<code>&#x7B2C;0&#x4E2A;&#x8F93;&#x51FA;</code>,<code>shape</code>&#x8868;&#x793A;<code>&#x7EF4;&#x5EA6;</code></p>
<p>&#x6CE8;&#x610F;vim</p>
<pre><code>vim ~/.vimrc&#x5199;&#x5165;&#xFF1A;
set ts=4    # tab&#x952E;&#x8F6C;&#x6210;4&#x4E2A;&#x7A7A;&#x683C;
set nu      # &#x663E;&#x793A;&#x884C;&#x53F7;
</code></pre><ul>
<li><strong>&#x8BA1;&#x7B97;&#x56FE;</strong>(<code>Graph</code>)&#xFF1A;&#x642D;&#x5EFA;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x8BA1;&#x7B97;&#x8FC7;&#x7A0B;&#xFF0C;&#x53EA;&#x642D;&#x5EFA;&#xFF0C;&#x4E0D;&#x8FD0;&#x7B97;&#x3002;</li>
</ul>
<p><img src="http://ovhbzkbox.bkt.clouddn.com/2018-07-17-15318034642927.jpg" width="200"></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
x = tf.constant([[<span class="hljs-number">1.0</span>,<span class="hljs-number">2.0</span>]])
w = tf.constant([[<span class="hljs-number">3.0</span>],[<span class="hljs-number">4.0</span>]])
y = tf.matmul(x,w)
print(y)
</code></pre>
<p>&#x663E;&#x793A;</p>
<pre><code class="lang-python">Tensor(<span class="hljs-string">&quot;matmul:0&quot;</span>,shape(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>),dtype=float32)
</code></pre>
<p>&#x4ECE;&#x8FD9;&#x91CC;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;<code>print</code>&#x7684;&#x7ED3;&#x679C;&#x663E;&#x793A;<code>y</code> &#x662F;&#x4E00;&#x4E2A;&#x5F20;&#x91CF;&#xFF0C;&#x53EA;&#x642D;&#x5EFA;&#x627F;&#x8F7D;&#x8BA1;&#x7B97;&#x8FC7;&#x7A0B;&#x7684;&#x8BA1;&#x7B97;&#x56FE;&#xFF0C;&#x5E76;&#x6CA1;&#x6709;&#x8FD0;&#x7B97;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x60F3;&#x5F97;&#x5230;&#x8FD0;&#x7B97;&#x7ED3;&#x679C;&#x5C31;&#x8981;&#x7528;&#x5230;&#x201C;<strong>&#x4F1A;&#x8BDD;</strong>&#x201C;<code>Session()</code>&#x4E86;&#x3002;</p>
<ul>
<li><strong>&#x4F1A;&#x8BDD;</strong>(<code>Session</code>)&#xFF1A;&#x6267;&#x884C;&#x8BA1;&#x7B97;&#x56FE;&#x4E2D;&#x7684;&#x8282;&#x70B9;&#x8FD0;&#x7B97;&#x3002;</li>
</ul>
<p>&#x6211;&#x4EEC;&#x7528;<code>with</code>&#x7ED3;&#x6784;&#x5B9E;&#x73B0;&#xFF0C;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-python"><span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
  print(sess.run(y))
</code></pre>
<p>&#x4E3E;&#x4F8B;</p>
<p>&#x5BF9;&#x4E8E;&#x521A;&#x521A;&#x6240;&#x8FF0;&#x8BA1;&#x7B97;&#x56FE;&#xFF0C;&#x6211;&#x4EEC;&#x6267;&#x884C;<code>Session()</code>&#x4F1A;&#x8BDD;&#x53EF;&#x5F97;&#x5230;&#x77E9;&#x9635;&#x76F8;&#x4E58;&#x7ED3;&#x679C;:</p>
<p>&#x8BA1;&#x7B97;<code>1.0*3.0+2.0*4.0=11.0</code></p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> tensorflow <span class="hljs-keyword">as</span> tf
x = tf.constant([[<span class="hljs-number">1.0</span>,<span class="hljs-number">2.0</span>]])
w = tf.constant([[<span class="hljs-number">3.0</span>],[<span class="hljs-number">4.0</span>]])
y = tf.matmul(x,w)
print(y)
<span class="hljs-keyword">with</span> tf.Session() <span class="hljs-keyword">as</span> sess:
    print(sess.run(y))
</code></pre>
<p>&#x7ED3;&#x679C;</p>
<pre><code class="lang-python">Tensor(<span class="hljs-string">&quot;matmul:0&quot;</span>,shape(<span class="hljs-number">1</span>,<span class="hljs-number">1</span>),dtype=float32)
[[<span class="hljs-number">11.</span>]]
</code></pre>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x8FD0;&#x884C;<code>Session()</code>&#x4F1A;&#x8BDD;&#x524D;&#x53EA;&#x6253;&#x5370;&#x51FA;<code>y</code>&#x662F;&#x4E2A;&#x5F20;&#x91CF;&#x7684;&#x63D0;&#x793A;&#xFF0C;&#x8FD0;&#x884C;<code>Session()</code>&#x4F1A;&#x8BDD;&#x540E;&#x6253;&#x5370;&#x51FA;&#x4E86;<code>y</code>&#x7684;&#x7ED3;&#x679C;<code>1.0*3.0 + 2.0*4.0 = 11.0</code>&#x3002;</p>
<blockquote>
<p><strong> [success]&#x6CE8;1 </strong>:
&#x6211;&#x4EEC;&#x4EE5;&#x540E;&#x4F1A;&#x5E38;&#x7528;&#x5230;<code>vim</code>&#x7F16;&#x8F91;&#x5668;&#xFF0C;&#x4E3A;&#x4E86;&#x4F7F;&#x7528;&#x65B9;&#x4FBF;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x66F4;&#x6539;<code>vim</code>&#x7684;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#xFF0C;&#x4F7F;<code>vim</code>&#x7684;&#x4F7F;&#x7528;&#x66F4;&#x52A0;&#x4FBF;&#x6377;&#x3002;&#x6211;&#x4EEC;&#x5728;<code>vim ~/.vimrc</code>&#x5199;&#x5165;:
<code>set ts=4</code>&#x8868;&#x793A;&#x4F7F;<code>Tab</code>&#x952E;&#x7B49;&#x6548;&#x4E3A;4&#x4E2A;&#x7A7A;&#x683C;
<code>set nu</code>&#x8868;&#x793A;&#x4F7F;<code>vim</code>&#x663E;&#x793A;&#x884C;&#x53F7;<code>nu</code>&#x662F;<code>number</code>&#x7F29;&#x5199;</p>
<p><strong> &#x6CE8;2 </strong>: &#x5728;<code>vim</code>&#x7F16;&#x8F91;&#x5668;&#x4E2D;&#x8FD0;&#x884C;<code>Session()</code>&#x4F1A;&#x8BDD;&#x65F6;&#xFF0C;&#x6709;&#x65F6;&#x4F1A;&#x51FA;&#x73B0;&#x63D0;&#x793A;<code>warning</code>&#xFF0C;&#x662F;&#x56E0;&#x4E3A;&#x6709;&#x7684;&#x7535;&#x8111;&#x53EF;&#x4EE5;&#x652F;&#x6301;&#x52A0;&#x901F;&#x6307;&#x4EE4;&#xFF0C;&#x4F46;&#x662F;&#x8FD0;&#x884C;&#x4EE3;&#x7801;&#x65F6;&#x5E76;&#x6CA1;&#x6709;&#x542F;&#x52A8;&#x8FD9;&#x4E9B;&#x6307;&#x4EE4;&#x3002;&#x53EF;&#x4EE5;&#x628A;&#x8FD9;&#x4E9B;&#x63D0;&#x793A;<code>warning</code>&#x6682;&#x65F6;&#x5C4F;&#x853D;&#x6389;&#x3002;&#x5C4F;&#x853D;&#x65B9;&#x6CD5;&#x4E3A;&#x8FDB;&#x5165;&#x4E3B;&#x76EE;&#x5F55;&#x4E0B;&#x7684;<code>bashrc</code>&#x6587;&#x4EF6;&#xFF0C;&#x5728;<code>bashrc</code>&#x6587;&#x4EF6;&#x4E2D;&#x52A0;&#x5165;&#x8FD9;&#x6837;&#x4E00;&#x53E5;<code>export TF_CPP_MIN_LOG_LEVEL=2</code>&#xFF0C;&#x4ECE;&#x800C;&#x628A;&#x63D0;&#x793A;<code>warning</code>&#x7B49;&#x7EA7;&#x964D;&#x4F4E;&#x3002;&#x8FD9;&#x4E2A;&#x547D;&#x4EE4;&#x53EF;&#x4EE5;&#x63A7;&#x5236;<code>python</code>&#x7A0B;&#x5E8F;&#x663E;&#x793A;&#x63D0;&#x793A;&#x4FE1;&#x606F;&#x7684;&#x7B49;&#x7EA7;&#xFF0C;&#x5728;<code>Tensorflow</code>&#x91CC;&#x9762;&#x4E00;&#x822C;&#x8BBE;&#x7F6E;&#x6210;&#x662F;&quot;0&quot;(&#x663E;&#x793A;&#x6240;&#x6709;&#x4FE1;&#x606F;)&#x6216;&#x8005;&quot;1&quot;(&#x4E0D;&#x663E;&#x793A;<code>info</code>)&#xFF0C;&quot;2&quot;&#x4EE3;&#x8868;&#x4E0D;&#x663E;&#x793A;<code>warning</code>&#xFF0C;&quot;3&quot;&#x4EE3;&#x8868;&#x4E0D;&#x663E;&#x793A; <code>error</code>&#x3002;&#x4E00;&#x822C;&#x4E0D;&#x5EFA;&#x8BAE;&#x8BBE;&#x7F6E;&#x6210; 3&#x3002;<code>source</code>&#x547D;&#x4EE4;&#x7528;&#x4E8E;&#x91CD;&#x65B0;&#x6267;&#x884C;&#x4FEE;&#x6539;&#x7684;&#x521D;&#x59CB;&#x5316;&#x6587;&#x4EF6;&#xFF0C;&#x4F7F;&#x4E4B;&#x7ACB;&#x5373;&#x751F;&#x6548;&#xFF0C;&#x800C;&#x4E0D;&#x5FC5;&#x6CE8;&#x9500;&#x5E76;&#x91CD;&#x65B0;&#x767B;&#x5F55;&#x3002;</p>
</blockquote>
<h2 id="&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x53C2;&#x6570;">&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x53C2;&#x6570;</h2>
<ul>
<li>&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;<strong>&#x53C2;&#x6570;</strong>: &#x662F;&#x6307;&#x795E;&#x7ECF;&#x5143;&#x7EBF;&#x4E0A;&#x7684;&#x6743;&#x91CD;<code>w</code>&#xFF0C;&#x7528;&#x53D8;&#x91CF;&#x8868;&#x793A;&#xFF0C;&#x4E00;&#x822C;&#x4F1A;&#x5148;&#x968F;&#x673A;&#x751F;&#x6210;&#x8FD9;&#x4E9B;&#x53C2;&#x6570;&#x3002;&#x751F;&#x6210;&#x53C2;&#x6570;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x8BA9;<code>w</code>&#x7B49;&#x4E8E;<code>tf.Variable</code>&#xFF0C;&#x628A;&#x751F;&#x6210;&#x7684;&#x65B9;&#x5F0F;&#x5199;&#x5728;&#x62EC;&#x53F7;&#x91CC;&#x3002;</li>
</ul>
<p>&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x4E2D;&#x5E38;&#x7528;&#x7684;&#x751F;&#x6210;&#x968F;&#x673A;&#x6570;/&#x6570;&#x7EC4;&#x7684;&#x51FD;&#x6570;&#x6709;:</p>
<table>
<thead>
<tr>
<th>&#x51FD;&#x6570;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>tf.random_normal()</code></td>
<td>&#x751F;&#x6210;&#x6B63;&#x6001;&#x5206;&#x5E03;&#x968F;&#x673A;&#x6570;</td>
</tr>
<tr>
<td><code>tf.truncated_normal()</code></td>
<td>&#x751F;&#x6210;&#x53BB;&#x6389;&#x8FC7;&#x5927;&#x504F;&#x79BB;&#x70B9;&#x7684;&#x6B63;&#x6001;&#x5206;&#x5E03;&#x968F;&#x673A;&#x6570;</td>
</tr>
<tr>
<td><code>tf.random_uniform()</code></td>
<td>&#x751F;&#x6210;&#x5747;&#x5300;&#x5206;&#x5E03;&#x968F;&#x673A;&#x6570;</td>
</tr>
<tr>
<td><code>tf.zeros</code></td>
<td>&#x8868;&#x793A;&#x751F;&#x6210;&#x5168; 0 &#x6570;&#x7EC4;</td>
</tr>
<tr>
<td><code>tf.ones</code></td>
<td>&#x8868;&#x793A;&#x751F;&#x6210;&#x5168; 1 &#x6570;&#x7EC4;</td>
</tr>
<tr>
<td><code>tf.fill</code></td>
<td>&#x8868;&#x793A;&#x751F;&#x6210;&#x5168;&#x5B9A;&#x503C;&#x6570;&#x7EC4;</td>
</tr>
<tr>
<td><code>tf.constant</code></td>
<td>&#x8868;&#x793A;&#x751F;&#x6210;&#x76F4;&#x63A5;&#x7ED9;&#x5B9A;&#x503C;&#x7684;&#x6570;&#x7EC4;</td>
</tr>
</tbody>
</table>
<p>&#x4E3E;&#x4F8B;</p>
<p>(1)&#x751F;&#x6210;&#x6B63;&#x6001;&#x5206;&#x5E03;&#x968F;&#x673A;&#x6570;&#xFF0C;&#x5F62;&#x72B6;&#x4E24;&#x884C;&#x4E09;&#x5217;&#xFF0C;&#x6807;&#x51C6;&#x5DEE;&#x662F;2&#xFF0C;&#x5747;&#x503C;&#x662F;0&#xFF0C;&#x968F;&#x673A;&#x79CD;&#x5B50;&#x662F;1&#x3002;</p>
<pre><code class="lang-python">w=tf.Variable(tf.random_normal([<span class="hljs-number">2</span>,<span class="hljs-number">3</span>],stddev=<span class="hljs-number">2</span>, mean=<span class="hljs-number">0</span>, seed=<span class="hljs-number">1</span>))
</code></pre>
<p>(2)&#x53BB;&#x6389;&#x504F;&#x79BB;&#x8FC7;&#x5927;&#x7684;&#x6B63;&#x6001;&#x5206;&#x5E03;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5982;&#x679C;&#x968F;&#x673A;&#x51FA;&#x6765;&#x7684;&#x6570;&#x636E;&#x504F;&#x79BB;&#x5E73;&#x5747;&#x503C;&#x8D85;&#x8FC7;&#x4E24;&#x4E2A;&#x6807;&#x51C6;&#x5DEE;&#xFF0C;&#x8FD9;&#x4E2A;&#x6570;&#x636E;&#x5C06;&#x91CD;&#x65B0;&#x751F;&#x6210;&#x3002;</p>
<pre><code class="lang-python">w=tf.Variable(tf.truncated_normal([<span class="hljs-number">2</span>,<span class="hljs-number">3</span>],stddev=<span class="hljs-number">2</span>, mean=<span class="hljs-number">0</span>, seed=<span class="hljs-number">1</span>))
</code></pre>
<p>(3)&#x4ECE;&#x4E00;&#x4E2A;&#x5747;&#x5300;&#x5206;&#x5E03;<code>[minval maxval)</code>&#x4E2D;&#x968F;&#x673A;&#x91C7;&#x6837;&#xFF0C;&#x6CE8;&#x610F;&#x5B9A;&#x4E49;&#x57DF;&#x662F;&#x5DE6;&#x95ED;&#x53F3;&#x5F00;&#xFF0C;&#x5373;&#x5305;&#x542B;<code>minval</code>&#xFF0C;&#x4E0D;&#x5305;&#x542B;<code>maxval</code>&#x3002;</p>
<pre><code class="lang-python">w=random_uniform(shape=[<span class="hljs-number">2</span>,<span class="hljs-number">3</span>],minval=<span class="hljs-number">0</span>,maxval=<span class="hljs-number">1</span>,dtype=tf.int32&#xFF0C;seed=<span class="hljs-number">1</span>)
</code></pre>
<p>(4)&#x9664;&#x4E86;&#x751F;&#x6210;&#x968F;&#x673A;&#x6570;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x751F;&#x6210;&#x5E38;&#x91CF;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x751F;&#x6210;[[0,0],[0,0],[0,0]]</span>
tf.zeros([<span class="hljs-number">3</span>,<span class="hljs-number">2</span>],tf.int32)

<span class="hljs-comment">#&#x751F;&#x6210;[[1,1],[1,1],[1,1]</span>
tf.ones([<span class="hljs-number">3</span>,<span class="hljs-number">2</span>],tf.int32)

<span class="hljs-comment"># &#x751F;&#x6210;[[6,6],[6,6],[6,6]]</span>
tf.fill([<span class="hljs-number">3</span>,<span class="hljs-number">2</span>],<span class="hljs-number">6</span>)

<span class="hljs-comment">#&#x751F;&#x6210;[3,2,1]</span>
tf.constant([<span class="hljs-number">3</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>])
</code></pre>
<blockquote>
<p><strong>[info]&#x6CE8;</strong>&#xFF1A;</p>
<ol>
<li><p>&#x968F;&#x673A;&#x79CD;&#x5B50;&#x5982;&#x679C;&#x53BB;&#x6389;&#x6BCF;&#x6B21;&#x751F;&#x6210;&#x7684;&#x968F;&#x673A;&#x6570;&#x5C06;&#x4E0D;&#x4E00;&#x81F4;&#x3002;</p>
</li>
<li><p>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7279;&#x6B8A;&#x8981;&#x6C42;&#x6807;&#x51C6;&#x5DEE;&#x3001;&#x5747;&#x503C;&#x3001;&#x968F;&#x673A;&#x79CD;&#x5B50;&#x662F;&#x53EF;&#x4EE5;&#x4E0D;&#x5199;&#x7684;&#x3002;</p>
</li>
</ol>
</blockquote>
<h2 id="&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x642D;&#x5EFA;">&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x642D;&#x5EFA;</h2>
<p>&#x5F53;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x5F20;&#x91CF;&#x3001;&#x8BA1;&#x7B97;&#x56FE;&#x3001;&#x4F1A;&#x8BDD;&#x548C;&#x53C2;&#x6570;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8BA8;&#x8BBA;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x5B9E;&#x73B0;&#x8FC7;&#x7A0B;&#x4E86;&#x3002;</p>
<ul>
<li>&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x5B9E;&#x73B0;&#x8FC7;&#x7A0B;:</li>
</ul>
<p>(1) &#x51C6;&#x5907;&#x6570;&#x636E;&#x96C6;&#xFF0C;&#x63D0;&#x53D6;&#x7279;&#x5F81;&#xFF0C;&#x4F5C;&#x4E3A;&#x8F93;&#x5165;&#x5582;&#x7ED9;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;(<code>Neural Network</code>&#xFF0C;<code>NN</code>)</p>
<p>(2) &#x642D;&#x5EFA;<code>NN</code>&#x7ED3;&#x6784;&#xFF0C;&#x4ECE;&#x8F93;&#x5165;&#x5230;&#x8F93;&#x51FA;(&#x5148;&#x642D;&#x5EFA;&#x8BA1;&#x7B97;&#x56FE;&#xFF0C;&#x518D;&#x7528;&#x4F1A;&#x8BDD;&#x6267;&#x884C;)
(<code>NN</code>&#x524D;&#x5411;&#x4F20;&#x64AD;&#x7B97;&#x6CD5; =&gt; &#x8BA1;&#x7B97;&#x8F93;&#x51FA;)</p>
<p>(3) &#x5927;&#x91CF;&#x7279;&#x5F81;&#x6570;&#x636E;&#x5582;&#x7ED9;<code>NN</code>&#xFF0C;&#x8FED;&#x4EE3;&#x4F18;&#x5316;<code>NN</code> &#x53C2;&#x6570;
(NN &#x53CD;&#x5411;&#x4F20;&#x64AD;&#x7B97;&#x6CD5; =&gt; &#x4F18;&#x5316;&#x53C2;&#x6570;&#x8BAD;&#x7EC3;&#x6A21;&#x578B;)</p>
<p>(4) &#x4F7F;&#x7528;&#x8BAD;&#x7EC3;&#x597D;&#x7684;&#x6A21;&#x578B;&#x9884;&#x6D4B;&#x548C;&#x5206;&#x7C7B;</p>
<p>&#x7531;&#x6B64;&#x53EF;&#x89C1;&#xFF0C;&#x57FA;&#x4E8E;&#x795E;&#x7ECF;&#x7F51;&#x7EDC;&#x7684;&#x673A;&#x5668;&#x5B66;&#x4E60;&#x4E3B;&#x8981;&#x5206;&#x4E3A;&#x4E24;&#x4E2A;&#x8FC7;&#x7A0B;&#xFF0C;&#x5373;&#x8BAD;&#x7EC3;&#x8FC7;&#x7A0B;&#x548C;&#x4F7F;&#x7528;&#x8FC7;&#x7A0B;&#x3002; &#x8BAD;&#x7EC3;&#x8FC7;&#x7A0B;&#x662F;&#x7B2C;&#x4E00;&#x6B65;&#x3001;&#x7B2C;&#x4E8C;&#x6B65;&#x3001;&#x7B2C;&#x4E09;&#x6B65;&#x7684;&#x5FAA;&#x73AF;&#x8FED;&#x4EE3;&#xFF0C;&#x4F7F;&#x7528;&#x8FC7;&#x7A0B;&#x662F;&#x7B2C;&#x56DB;&#x6B65;&#xFF0C;&#x4E00;&#x65E6;&#x53C2;&#x6570; &#x4F18;&#x5316;&#x5B8C;&#x6210;&#x5C31;&#x53EF;&#x4EE5;&#x56FA;&#x5B9A;&#x8FD9;&#x4E9B;&#x53C2;&#x6570;&#xFF0C;&#x5B9E;&#x73B0;&#x7279;&#x5B9A;&#x5E94;&#x7528;&#x4E86;&#x3002;
&#x5F88;&#x591A;&#x5B9E;&#x9645;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5148;&#x4F7F;&#x7528;&#x73B0;&#x6709;&#x7684;&#x6210;&#x719F;&#x7F51;&#x7EDC;&#x7ED3;&#x6784;&#xFF0C;&#x5582;&#x5165;&#x65B0;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x8BAD;&#x7EC3;&#x76F8;&#x5E94; &#x6A21;&#x578B;&#xFF0C;&#x5224;&#x65AD;&#x662F;&#x5426;&#x80FD;&#x5BF9;&#x5582;&#x5165;&#x7684;&#x4ECE;&#x672A;&#x89C1;&#x8FC7;&#x7684;&#x65B0;&#x6570;&#x636E;&#x4F5C;&#x51FA;&#x6B63;&#x786E;&#x54CD;&#x5E94;&#xFF0C;&#x518D;&#x9002;&#x5F53;&#x66F4;&#x6539;&#x7F51;&#x7EDC;&#x7ED3; &#x6784;&#xFF0C;&#x53CD;&#x590D;&#x8FED;&#x4EE3;&#xFF0C;&#x8BA9;&#x673A;&#x5668;&#x81EA;&#x52A8;&#x8BAD;&#x7EC3;&#x53C2;&#x6570;&#x627E;&#x51FA;&#x6700;&#x4F18;&#x7ED3;&#x6784;&#x548C;&#x53C2;&#x6570;&#xFF0C;&#x4EE5;&#x56FA;&#x5B9A;&#x4E13;&#x7528;&#x6A21;&#x578B;&#x3002;</p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; scottdu 2018 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2018-09-18 19:09:43
</span></footer> <link rel="stylesheet" type="text/css" href="https://storage.googleapis.com/app.klipse.tech/css/codemirror.css"> <script>     window.klipse_settings = {         selector: ".language-klipse, .lang-eval-clojure",         selector_eval_js: ".lang-eval-js",         selector_eval_python_client: ".lang-eval-python",         selector_eval_php: ".lang-eval-php",         selector_eval_scheme: ".lang-eval-scheme",         selector_eval_ruby: ".lang-eval-ruby",         selector_reagent: ".lang-reagent",        selector_google_charts: ".lang-google-chart",        selector_es2017: ".lang-eval-es2017",        selector_jsx: ".lang-eval-jsx",        selector_transpile_jsx: ".lang-transpile-jsx",        selector_render_jsx: ".lang-render-jsx",        selector_react: ".lang-react",        selector_eval_markdown: ".lang-render-markdown",        selector_eval_lambdaway: ".lang-render-lambdaway",        selector_eval_cpp: ".lang-eval-cpp",        selector_eval_html: ".lang-render-html",        selector_sql: ".lang-eval-sql",        selector_brainfuck: "lang-eval-brainfuck",        selector_js: ".lang-transpile-cljs"    }; </script> <script src="https://storage.googleapis.com/app.klipse.tech/plugin/js/klipse_plugin.js"></script>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev " aria-label="Previous page: 第一章 Tensorflow框架">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="section1.2.html" class="navigation navigation-next " aria-label="Next page: 第二节 前向传播">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"第一节 张量、计算图、会话","level":"1.2.1","depth":2,"next":{"title":"第二节 前向传播","level":"1.2.2","depth":2,"path":"chapter1/section1.2.md","ref":"chapter1/section1.2.md","articles":[]},"previous":{"title":"第一章 Tensorflow框架","level":"1.2","depth":1,"path":"chapter1/README.md","ref":"chapter1/README.md","articles":[{"title":"第一节 张量、计算图、会话","level":"1.2.1","depth":2,"path":"chapter1/section1.1.md","ref":"chapter1/section1.1.md","articles":[]},{"title":"第二节 前向传播","level":"1.2.2","depth":2,"path":"chapter1/section1.2.md","ref":"chapter1/section1.2.md","articles":[]},{"title":"第三节 反向传播","level":"1.2.3","depth":2,"path":"chapter1/section1.3.md","ref":"chapter1/section1.3.md","articles":[]},{"title":"第四节 搭建神经网络的步骤","level":"1.2.4","depth":2,"path":"chapter1/section1.4.md","ref":"chapter1/section1.4.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["katex","expandable-chapters-small","tbfed-pagefooter","alerts","copy-code-button","puml","graph","chart","klipse","donate","simple-page-toc","splitter"],"root":".","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy scottdu 2018","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"puml":{},"simple-page-toc":{"maxDepth":3,"skipFirstH1":true},"splitter":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"graph":{},"donate":{"alipay":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-alipay1.jpeg","alipayText":"支付宝打赏","button":"赏","title":"","wechat":"http://ovhbzkbox.bkt.clouddn.com/2018-08-11-wechatpay.png","wechatText":"微信打赏"},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"alerts":{},"expandable-chapters-small":{},"copy-code-button":{},"klipse":{"myConfigKey":"it's the default value"},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"chart":{"type":"c3"}},"theme":"default","author":"scottdu","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Tensorflow学习笔记","language":"zh-hans","gitbook":"3.2.3","description":"记录Tensorflow的学习内容"},"file":{"path":"chapter1/section1.1.md","mtime":"2018-09-18T11:09:43.338Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-09-25T05:55:22.995Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

